package com.spark.common.widget

import android.content.Context
import android.support.v4.view.ViewPager
import android.util.AttributeSet
import android.view.ViewGroup

class NestViewPager(context: Context, attrs: AttributeSet?) : ViewPager(context, attrs) {
    constructor(context: Context) : this(context, null)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var height = 0
        var newHeightSpec: Int
        for (i in 0 until childCount) {
            val child = getChildAt(i) as ViewGroup
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(MeasureSpec.UNSPECIFIED), MeasureSpec.UNSPECIFIED))
            val childHeight = child.measuredHeight
            if (childHeight > height) height = childHeight
        }
        newHeightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
        super.onMeasure(widthMeasureSpec, newHeightSpec)
    }
}